﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ma_Di_Tuan.Properties;
using System.Diagnostics;
using System.Media;
using System.IO;

namespace Ma_Di_Tuan
{
    public partial class Board : UserControl
    {

        private string[]        outPut = new string[5];
        private TimeStruct        timeRun;                 //time
        private int             size;                      //Kích thước bàn cờ
        private PictureBox[,]   pbaBoard;
        private int             initX, x;
        private int             initY, y;
        private int[,]          vt = new int[2, 2501];          //Mảng lưu Vị trí của quân Mã
        private int             maxSize = 50;
        private bool            isStop = false;                   //Tuy chon Start hay Stop
        private int             step = 0;                       //So step di cua Ma

        const String START = "Bắt đầu";
        const String PAUSE = "Tạm dừng";
        const String CONTINUE = "Tiếp tục";

        public Board()
        {
            InitializeComponent();

            cWaittingTime.Items.Add("0.5");
            for (int i = 1; i < 6; i++)
            {
                cWaittingTime.Items.Add(i);
            }
            cWaittingTime.Text = Settings.Default.strTime;

            this.bStart.Text = START;
            this.bStart.Focus();
        }

        public void SetValue(int _initX,int _initY,int pSize,PictureBox[,] ppbaBoard)
        {
            initX = _initX;
            initY = _initY;
            size = pSize;
            pbaBoard = ppbaBoard;
        }

        public string GetTimeCombobox()
        {
            return this.cWaittingTime.Text;
        }

        //Init controls
        public void InitControl()
        {
            //Calculate
            Algorithm algorithm = new Algorithm();
            algorithm.SetGT(initX, initY, size);
            algorithm.DiTuan();
            vt = algorithm._vt;

            this.Top                    =   20;
            this.Left                   =   30 + 17 + maxSize * size;
            this.lStepNo.Text           +=  "1";

            //Load option saved
            this.cWaittingTime.Text         = Settings.Default.strTime;

            //Put first horse
            pbaBoard[initX, initY].Image = global::Ma_Di_Tuan.Properties.Resources.red_horse;
            if (this.cWaittingTime.Text != "0.5" && int.Parse(this.cWaittingTime.Text) > 1)
            {
                pbaBoard[vt[0, 1], vt[1, 1]].Image = global::Ma_Di_Tuan.Properties.Resources.cu_horse;
            }

            //Lưu kết quả vào mảng outPut để tiện cho lúc xuất file
            outPut[0]   = "Kích thước bàn cờ: " + size.ToString() + " x " + size.ToString();
            outPut[1] = "Tọa độ ban đầu: (" + initX.ToString() + "; " + initY.ToString() + ")";

            x           = initX;
            y           = initY;
            step        = 1;
            isStop        = false;

            //init time struct
            timeRun      = new TimeStruct();
            timeRun.hour = 0;
            timeRun.minute  = 0;
            timeRun.second  = 0;
        }

        public void PlaySound(System.IO.UnmanagedMemoryStream fileName)
        {
            SoundPlayer sound = new SoundPlayer(fileName);
            sound.LoadAsync();
            sound.Play();
        }

        //Delete all horse
        private void RemoveAllHorse()
        {
            for (int i = 1; i <= size; i++)
                for (int j = 1; j <= size; j++)
                    pbaBoard[i, j].Image = null;
        }

        private void bNew_Click(object sender, EventArgs e)
        {
            //Đưa ra thông báo xác nhận
            DialogResult a = MessageBox.Show("Bạn có muốn khởi tạo lại ?",
                                            "Thông báo",
                                            MessageBoxButtons.YesNo,
                                            MessageBoxIcon.Question,
                                            MessageBoxDefaultButton.Button1);

            if (a == DialogResult.Yes)
            {
                Process.Start(Application.ExecutablePath);
                Application.Exit();
            }
        }

        private void bStart_Click(object sender, EventArgs e)
        {
            if (this.bStart.Text == START)
            {
                timeRun      = new TimeStruct();
                timeRun.hour = 0;
                timeRun.minute  = 0;
                timeRun.second  = 0;
            }

            if (isStop == false)
            {
                isStop = true;
                this.bStart.Text = PAUSE;
                timer1.Enabled = true;
                if (step == 1)
                {
                    timer2.Enabled = true;
                }

                cboTimeStructCho_SelectedIndexChanged(null, null);

                this.bNew.Enabled = false;
            }
            else
            {
                isStop                    = false;
                this.bStart.Text      = CONTINUE;
                this.bNew.Enabled    = true;
                timer1.Enabled          = false;
                timer2.Enabled          = false;
            }
        }

        private void bAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Trường Đại học công nghệ thông tin\n" +
                    "Khoa công nghệ phần mềm\n" +
                    "Môn : Một số thuật toán thông minh\n" +
                    "\nGiảng viên : PGS/TS. Vũ Thanh Nguyên\n" +
                    "\nSinh viên : Nguyễn Công Huy\n" +
                    "MSSV: 08520148\n" +
                    "Email: conghuy2012@gmail.com");
        }

        private void cboTimeStructCho_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (this.cWaittingTime.Text == "0.5")
            {
                timer1.Interval = 500;
            }
            else
            {
                timer1.Interval = int.Parse(this.cWaittingTime.Text) * 1000;
            }

            if (this.cWaittingTime.Text != "0.5" && int.Parse(this.cWaittingTime.Text) > 1 && step == 1)
            {
                pbaBoard[vt[0, 1], vt[1, 1]].Image = global::Ma_Di_Tuan.Properties.Resources.cu_horse;
            }
            else
            {
                if (step == 1)
                    pbaBoard[vt[0, 1], vt[1, 1]].Image = null;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (step == 0)
            {
                //Nếu bước == 0 thì đi tuần lại
                step++;
                RemoveAllHorse();
                timer2.Enabled              = true;
                pbaBoard[initX, initY].Image   = global::Ma_Di_Tuan.Properties.Resources.red_horse;
            }
            else
            {
                //if step != 0, let's go.
                pbaBoard[x, y].Image   = global::Ma_Di_Tuan.Properties.Resources.white_horse;
                x                   = vt[0, step];
                y                   = vt[1, step];
                pbaBoard[x, y].Image   = global::Ma_Di_Tuan.Properties.Resources.red_horse;

                this.lStepNo.Text   = "Bước : " + (step + 1).ToString();
                step++;


                //finish ?
                if (step == size * size)
                {
                    isStop                = false;
                    timer2.Enabled      = false;
                    timer1.Enabled      = false;
                    this.bStart.Text  = START;

                    //set to out put
                    outPut[2] = "Tọa Độ Kết Thúc: ( " + x.ToString() + "; " + y.ToString() + ")";
                    outPut[3] = "Tổng Số Bước: " + step.ToString();
                    outPut[4] = "Thời Gian Hoàn Thành: " + timeRun.hour.ToString() + " giờ " +
                                timeRun.minute.ToString() + " phút " + timeRun.second.ToString() + " giây";

                    RemoveAllHorse();
                    pbaBoard[initX, initY].Image = global::Ma_Di_Tuan.Properties.Resources.red_horse;
                    if (this.cWaittingTime.Text != "0.5" && int.Parse(this.cWaittingTime.Text) > 1)
                        pbaBoard[vt[0, 1], vt[1, 1]].Image = global::Ma_Di_Tuan.Properties.Resources.cu_horse;

                    //reset
                    step                    = 0;
                    x                       = initX;
                    y                       = initY;

                    this.bNew.Enabled    = true;
                    this.lStepNo.Text       = "Bước : 1";
                    step++;
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (timeRun.second++ >= 59)
            {
                timeRun.second = 0;
                timeRun.minute++;
                if (timeRun.minute >= 59)
                {
                    timeRun.minute = 0;
                    timeRun.hour++;
                }
            }

            if (this.cWaittingTime.Text != "0.5" && int.Parse(this.cWaittingTime.Text) > 1)
                pbaBoard[vt[0, step], vt[1, step]].Image = global::Ma_Di_Tuan.Properties.Resources.cu_horse;
        }


    }
} 
